package edu.drexel.cs575.phw1.scale;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import listeners.TrayListener;

/**
 * Interacts with <code>Weighable</code> items.
 * 
 * @author anthony
 *
 */
public class Tray {
	private List<Weighable> items = new ArrayList<Weighable>();
	private List<TrayListener> listeners = new ArrayList<TrayListener>();
	
	public void addItem(Weighable item) {
		this.items.add(item);
		
		System.out.println("TRAY: Item added");
		notifyItemAddedListeners();
	}
	
	/**
	 * Removes <b>all</b> items from the tray.
	 */
	public void removeItems() {
		this.items.clear();
		
		System.out.println("TRAY: All items removed");
		notifyItemsRemovedListeners();
	}
	
	public List<Weighable> getItemsInTray() {
		return Collections.unmodifiableList(items);
	}
	
	public void addListener(TrayListener listener) {
		if (listener == null) {
			throw new IllegalArgumentException("Listener cannot be null");
		}
		
		this.listeners.add(listener);
	}
	
	private void notifyItemAddedListeners() {
		for(TrayListener listener : listeners) {
			listener.itemAdded();
		}
	}
	
	private void notifyItemsRemovedListeners() {
		for(TrayListener listener : listeners) {
			listener.itemsRemoved();
		}
	}
}
